package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 * 
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    @Autowired
    private TbBusinessMapper tbBusinessMapper;
    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;

    /**
     * 新增商机跟进记录
     * @param businessTrackVo
     */
    @Override
    public void add(BusinessTrackVo businessTrackVo) {
        //修改商机表信息
        TbBusiness tbBusiness = new TbBusiness();
        BeanUtils.copyProperties(businessTrackVo,tbBusiness);
        tbBusiness.setId(businessTrackVo.getBusinessId());
        tbBusiness.setStatus("2");
        tbBusinessMapper.updateTbBusiness(tbBusiness);
        //新增商机跟进记录信息
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        BeanUtils.copyProperties(businessTrackVo,tbBusinessTrackRecord);

        tbBusinessTrackRecord.setCreateBy(SecurityUtils.getUsername());

        LocalDateTime dateTime = LocalDateTime.now();
        ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.systemDefault());
        Instant instant2 = zonedDateTime.toInstant();
        Date date = Date.from(instant2);

        tbBusinessTrackRecord.setCreateTime(date);

        tbBusinessTrackRecordMapper.add(tbBusinessTrackRecord);

    }

    /**
     * 商机列表查询
     * @param id
     * @return
     */
    @Override
    public List<TbBusinessTrackRecord> list(Long id) {
        return tbBusinessTrackRecordMapper.list(id);
    }

    /**
     * 退回公海
     * @param id
     * @param reson
     */
    @Override
    public void back(Long id, Integer reson) {
        //根据id查询商机
        TbBusiness tbBusiness = tbBusinessMapper.selectTbBusinessById(id);
        //添加商机跟进记录的record
        String record = tbBusinessMapper.getRecord(reson);
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();

        BeanUtils.copyProperties(tbBusiness,tbBusinessTrackRecord);
        tbBusinessTrackRecord.setId(null);
        tbBusinessTrackRecord.setBusinessId(tbBusiness.getId());
        tbBusinessTrackRecord.setRecord(record);

        tbBusinessTrackRecordMapper.add(tbBusinessTrackRecord);
        //修改商机状态
        tbBusiness.setStatus("3");
        tbBusinessMapper.updateTbBusiness(tbBusiness);

    }
}
